package com.wxd.woff.table;

import com.wxd.woff.AbstractOTFDataStream;
import com.wxd.woff.AbstractWoffTable;
import com.wxd.woff.ByteBufferOTFDataStream;
import com.wxd.woff.WoffFont;

import java.io.IOException;
import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 *
 * @author: wangxindong
 * @date: 2018/9/2-10:03
 */
public class HorizontalMetricsTable extends AbstractWoffTable {

    public static final String TAG = "hmtx";

    private int[] advanceWidth;
    private short[] leftSideBearing;
    private short[] nonHorizontalLeftSideBearing;
    private int numHMetrics;

    public HorizontalMetricsTable(WoffFont woffFont) {
        super(woffFont);
    }

    @Override
    public void read(WoffFont woffFont, AbstractOTFDataStream stream) throws IOException {
        stream.seek(offset);
        byte[] fontData = stream.read(compLength);
        byte[] inflateFont = inflateFontData(fontData);
        AbstractOTFDataStream data = new ByteBufferOTFDataStream(inflateFont);
        HorizontalHeaderTable hHeader = woffFont.getHorizontalHeader();
        numHMetrics = hHeader.getNumberOfHMetrics();

        int numGlyphs = woffFont.getNumberOfGlyphs();
        int bytesRead = 0;
        advanceWidth = new int[numHMetrics];
        leftSideBearing = new short[numHMetrics];
        for (int i = 0; i < numHMetrics; i++) {
            advanceWidth[i] = data.readUnsignedShort();
            leftSideBearing[i] = data.readSignedShort();
            bytesRead += 4;
        }

        int numberNonHorizontal = numGlyphs - numHMetrics;

        if (numberNonHorizontal < 0) {
            numberNonHorizontal = numGlyphs;
        }
        nonHorizontalLeftSideBearing = new short[numberNonHorizontal];

        if (bytesRead < origLength) {
            for (int i = 0; i < numberNonHorizontal; i++) {
                if (bytesRead < origLength) {
                    nonHorizontalLeftSideBearing[i] = data.readSignedShort();
                    bytesRead += 2;
                }
            }
        }
    }

    public int[] getAdvanceWidth() {
        return advanceWidth;
    }

    public void setAdvanceWidth(int[] advanceWidth) {
        this.advanceWidth = advanceWidth;
    }

    public short[] getLeftSideBearing() {
        return leftSideBearing;
    }

    public void setLeftSideBearing(short[] leftSideBearing) {
        this.leftSideBearing = leftSideBearing;
    }

    public short[] getNonHorizontalLeftSideBearing() {
        return nonHorizontalLeftSideBearing;
    }

    public void setNonHorizontalLeftSideBearing(short[] nonHorizontalLeftSideBearing) {
        this.nonHorizontalLeftSideBearing = nonHorizontalLeftSideBearing;
    }

    public int getNumHMetrics() {
        return numHMetrics;
    }

    public void setNumHMetrics(int numHMetrics) {
        this.numHMetrics = numHMetrics;
    }

    public int getAdvanceWidth(int gid) {
        if (gid < numHMetrics) {
            return advanceWidth[gid];
        } else {
            return advanceWidth[advanceWidth.length - 1];
        }
    }

    public int getLeftSideBearing(int gid) {
        if (gid < numHMetrics) {
            return leftSideBearing[gid];
        } else {
            return nonHorizontalLeftSideBearing[gid - numHMetrics];
        }
    }

    @Override
    public String toString() {
        return "AbstractWoffTable{" +
                "tag='" + tag + '\'' +
                ", offset=" + offset +
                ", compLength=" + compLength +
                ", origLength=" + origLength +
                ", origChecksum=" + origChecksum +
                ", advanceWidth=" + Arrays.toString(advanceWidth) +
                ", leftSideBearing=" + Arrays.toString(leftSideBearing) +
                ", nonHorizontalLeftSideBearing=" + Arrays.toString(nonHorizontalLeftSideBearing) +
                ", numHMetrics=" + numHMetrics +
                '}';
    }
}
